package cn.vehicle.biz.schedule;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.vehicle.domains.bo.QuerySnapshotBo;
import cn.vehicle.domains.entity.CameraSnapshotEntity;
import cn.vehicle.service.CameraSnapshotService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class DeleteSnapSchedule {

    @Resource
    private CameraSnapshotService cameraSnapshotService;

    /**
     * 定时删除快照数据
     * <p>
     * 删除快照数据，每 10分钟执行一次 删除30分钟前的数据
     * </p>
     */
    @Scheduled(fixedRate = 10, timeUnit = TimeUnit.MINUTES)
    public void deleteSnap() {
        log.info("定时删除快照数据开始");
        QuerySnapshotBo querySnapshotBo = new QuerySnapshotBo();
        querySnapshotBo.setSnapshotTimeStart(DateUtil.offset(new Date(), DateField.MINUTE, -40));
        querySnapshotBo.setSnapshotTimeEnd(DateUtil.offset(new Date(), DateField.MINUTE, -20));
        List<CameraSnapshotEntity> list = cameraSnapshotService.queryBySnapshotBo(querySnapshotBo);

        log.info("需要删除快照数据：{}", list.size());
        if (CollectionUtil.isEmpty(list)) {
            return;
        }


        for (CameraSnapshotEntity entity : list) {
            cameraSnapshotService.deleteSnapshot(entity);
        }

        log.info("定时删除快照数据结束");
    }
}
